package com.example.springboot.service.impl;

import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Book;
import com.example.springboot.entity.ReturnBook;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.BorrowMapper;
import com.example.springboot.mapper.ReturnBookMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.IReturnBookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ReturnBookService implements IReturnBookService {

    @Resource
    ReturnBookMapper returnBookMapper;

    @Resource
    BorrowMapper borrowMapper;

    @Resource
    BookMapper bookMapper;

    @Resource
    UserMapper userMapper;


    //查询所有
    @Override
    public List<ReturnBook> list() {
        return returnBookMapper.list();
    }

    //分页
    @Override
    public PageInfo<ReturnBook> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<ReturnBook> borrows = returnBookMapper.listByCondition(baseRequest);

        return new PageInfo<>(borrows);
    }

    //新增
    @Override
    @Transactional
    public void save(ReturnBook returnBook) {
        Date date = new Date();
        returnBook.setRealTime(LocalDate.now());
        returnBook.setStatus("已归还");
        borrowMapper.updateStatus("已归还", returnBook.getId());
        //修改图书数量
        returnBook.setId(null); // 还书的ID和借书ID要区分开，还书不等于借书的ID，
        returnBook.setCreateTime(date);
        returnBookMapper.save(returnBook);
        returnBookMapper.saves(returnBook);
        bookMapper.updateByISBN(returnBook.getBookIsbn());
        Book book = bookMapper.getByISBN(returnBook.getBookIsbn());
        if (book != null) {
            long util = 0;
            if (returnBook.getRealTime().isBefore(returnBook.getReturnTime())) {
                util = returnBook.getRealTime().until(returnBook.getReturnTime(), ChronoUnit.DAYS);
            } else if (returnBook.getRealTime().isAfter(returnBook.getReturnTime())) {
                util = -returnBook.getReturnTime().until(returnBook.getRealTime(), ChronoUnit.DAYS);
            }
            int score = (int) util * book.getScore();
            User user = userMapper.getById(returnBook.getUserId());
            int have = user.getScore();
            int a = score + have;
            user.setScore(a);
            if (a < 0 ){
                user.setVipID(false);
            }
            userMapper.save(user);
        }
    }

    @Override
    public ReturnBook getById(Integer id) {
        return returnBookMapper.getById(id);
    }

    //删除
    @Override
    public void deleteById(Integer id) {
        returnBookMapper.deleteById(id);
    }
}
